/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package sonia.golf.view;

//~--- non-JDK imports --------------------------------------------------------

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import sonia.golf.Course;
import sonia.golf.Hole;
import sonia.golf.R;
import sonia.golf.dao.CourseDAO;
import sonia.golf.dao.DAOFactory;

//~--- JDK imports ------------------------------------------------------------

import java.util.List;

/**
 *
 * @author Sebastian Sdorra
 */
public class CourseDetailView extends AbstractGolfView
{

  /** Field description */
  private static final short DEFAULT_HOLE_NUMBER = 3;

  //~--- constructors ---------------------------------------------------------

  /**
   * Constructs ...
   *
   *
   * @param activity
   * @param course
   */
  public CourseDetailView(Activity activity, Course course)
  {
    super(activity);
    this.course = course;
    this.courseDAO = DAOFactory.getInstance().getCourseDAO();
  }

  //~--- methods --------------------------------------------------------------

  /**
   * Method description
   *
   */
  public void show()
  {
    if (course.getHoleCount() == 0)
    {
      course.addHole(new Hole(currentHoleNumber, DEFAULT_HOLE_NUMBER));
    }

    TextView courseName = (TextView) activity.findViewById(R.id.course_name);

    courseName.setText(course.getName());

    final HoleArrayAdapter holeListAdapter =
      new HoleArrayAdapter(course.getHoles());
    ListView holeList = (ListView) activity.findViewById(R.id.hole_list);

    holeList.setAdapter(holeListAdapter);

    Button newHoleButton = (Button) activity.findViewById(R.id.new_hole);

    newHoleButton.setOnClickListener(new OnClickListener()
    {
      public void onClick(View view)
      {
        currentHoleNumber++;
        course.addHole(new Hole(currentHoleNumber, DEFAULT_HOLE_NUMBER));
        holeListAdapter.notifyDataSetChanged();
      }
    });
  }

  //~--- get methods ----------------------------------------------------------

  /**
   * Method description
   *
   *
   * @return
   */
  public Course getCourse()
  {
    return course;
  }

  /**
   * Method description
   *
   *
   * @return
   */
  public int getLayoutId()
  {
    return R.layout.coursedetailview;
  }

  //~--- inner classes --------------------------------------------------------

  /**
   * Class description
   *
   *
   * @version        Enter version here..., 10/08/30
   * @author         Enter your name here...
   */
  private class HoleArrayAdapter extends ArrayAdapter<Hole>
  {

    /**
     * Constructs ...
     *
     *
     * @param holes
     */
    public HoleArrayAdapter(List<Hole> holes)
    {
      super(activity, R.layout.doublelistitem, holes);
    }

    //~--- get methods --------------------------------------------------------

    /**
     * Method description
     *
     *
     * @param position
     * @param convertView
     * @param parent
     *
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
      View v = convertView;

      if (v == null)
      {
        LayoutInflater li = activity.getLayoutInflater();

        v = li.inflate(R.layout.doublelistitem, parent, false);
      }

      Hole hole = getItem(position);
      TextView left = (TextView) v.findViewById(R.id.list_item_left);

      left.setText(String.valueOf(hole.getNumber()));

      TextView right = (TextView) v.findViewById(R.id.list_item_right);

      right.setText(String.valueOf(hole.getPar()));

      return v;
    }
  }


  //~--- fields ---------------------------------------------------------------

  /** Field description */
  private Course course;

  /** Field description */
  private CourseDAO courseDAO;

  /** Field description */
  private short currentHoleNumber = 1;
}
